How to Generate Dynamic QR Code with Razorpay API in PHP

Generate Dynamic QR Code with Razorpay API in PHP

Razorpay is a popular payment gateway that allows businesses to accept online payments seamlessly. One of its features includes generating dynamic QR codes for accepting payments. In this tutorial, we’ll walk through the process of generating a dynamic QR code using Razorpay API in PHP.

Also Read: A Step-by-Step Guide: Integrate Razorpay payment gateway to your PHP website

Generating dynamic QR codes with Razorpay API in PHP offers a convenient solution for accepting online payments. By following the steps outlined in this guide, you can seamlessly integrate QR code generation into your PHP application, enhancing the payment experience for your customers.

A Step-by-Step Guide: Generate Dynamic QR Code with Razorpay API in PHP

Step 1: Razorpay Account:

You need to have a Razorpay account. Sign up at Razorpay if you don’t have one.

Razorpay Payment Gateway Integration

Step 2: Razorpay API Keys

To authenticate requests to the Razorpay API, you’ll need your API keys.

Log in to your Razorpay Dashboard and navigate to Settings > API Keys. Here, you’ll find your “Key ID” and “Key Secret.” Copy these credentials as you’ll need them in your PHP script to interact with the Razorpay API securely.

Razorpay API Keys

Create an auth.php file and Copy and Paste the following code

Step 3: Create your HTML form to get the customer details

Create an index.php page and Copy and Paste the following code into your index.php file.

Simple Steps to Integrate Razorpay Payments to Your PHP Website

Now add the below script code in the index.php file, this code will act on the form submit button click.

In the above JS code, When the user clicks on the pay now button we will get the customer details like name, email, mobile, and price using JS. Also, you can see the Ajax code to process the Razorpay payment. When payment succeeds or fails you will get redirected to the page based on the above script redirection.

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Step 4: Write the Ajax to Create Dynamic QR using Razorpay API

In this step, we’ve created the ‘submitpayment.php’ code file to manage AJAX post requests.

In the above code,

we are using above API to create customer on Razorpay

We are using above API to create dynamic QR code. Also you can check QR Code details in Razorpay account.

Generate Dynamic QR Code with Razorpay API in PHP

Step 5: Create payNow.php file

After successfully created QR image link, we will redirect to payNow.php with parameters “qr_id”.

Create payNow.php file and copy and paste following code:

In the above code, we are using below api link to fetch qr code details using qr_id.

When we will get QR code image url, we need to Embed image url to <img> element to display qr code as below image.

Also we are using setInterval function to check payment status every second. we are using “checkPaymentStatus.php” url as ajax to fetch payment status details.

Create checkPaymentStatus.php file and copy and paste below code

If payment captured successfully then you will redirect to payment success page.

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Step 6: Create a Payment Success Page

Use the below payment-success.php file code

That’s it. Now here you can add your own custom code to display success page layout and design.

Wrapping Words

In this tutorial, we learned how to generate a dynamic QR code using Razorpay API in PHP. Dynamic QR codes provide a convenient way to accept payments by allowing customers to scan the code and complete the transaction instantly. With Razorpay, integrating payment solutions into your PHP application becomes straightforward and efficient.

Do you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request

FAQs

What is a dynamic QR code, and why should I use it?

A dynamic QR code is a type of QR code that can change its details, typically used for transactions where the amount or details vary. Using dynamic QR codes with Razorpay API ensures flexibility and security in processing payments.

Do I need to have a Razorpay account to generate dynamic QR codes?

Yes, you need to have a Razorpay account to access their API and generate dynamic QR codes. Signing up for an account is free and can be done on their website.

Can I integrate dynamic QR code generation into my existing PHP application?

Yes, Razorpay’s API is designed for easy integration into PHP applications.

How to create a Razorpay account?

If you don’t have an account, sign up for a Razorpay account at https://dashboard.razorpay.com/

How to Get Razorpay API Keys?

Log in to your Razorpay Dashboard and navigate to Account & Settings > API Keys. Here, you’ll find your “Key ID” and “Key Secret.” 

Related posts